package com.wallace.downloadserver.interceptor

import com.wallace.downloadserver.annotation.ForbidMultipleCalls
import org.springframework.stereotype.Component
import org.springframework.web.method.HandlerMethod
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @Author: Wallace
 * @Description: 带有@ForbidMultipleCalls注解的方法不可多次调用，拦截超过调用限制的请求
 * @Date: 2022/1/20 1:10
 * @Modified By:
 */
@Component
class MultipleCallInterceptor : HandlerInterceptor {

    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
        // 请求的方法是否有注解
        if (handler.javaClass.isAssignableFrom(HandlerMethod::class.java)) {
            val handlerMethod = handler as HandlerMethod
            // 是否有@ForbidMultipleCalls注解
            if (handlerMethod.getMethodAnnotation(ForbidMultipleCalls::class.java) != null) {
                // TODO 处理逻辑
                return true
            }
        }
        return true
    }
}